<?php

namespace App\Admin\Controllers;

use App\Models\Page;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class PageController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Page::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->title('Title');
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Page::class, function (Form $form) {
            $form->display('id', 'ID');
            // $form->checkbox('ID')->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);
            $form->text('title', 'Title');
            $form->editor('content', 'Content');
            $form->text('slug', 'Slug');
            $form->hidden('author_id')->value(Admin::user()->id);
            $form->switch('is_publish', 'Publish?')->default(true);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');


            if (!$id = $form->model()->id) {
                return 'unique:users,email_address';
            }
        });
    }

    // public function downloadFile($type='pdf')
    // {
    //     switch ($type) {
    //         case 'pdf':
    //             $path = public_path($article->pdf)
    //             break;

    //         default:
    //             # code...
    //             break;
    //     }


    //     return response(, [
    //         'content-type' => 'xxxx'
    //     ])
    // }
}
